import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import activitypost from '../components/activitypost.vue';
import superadmin from '../components/superadmin.vue';
import admin from '../components/admin.vue';
import user from '../components/user.vue';
import activitylist from '../components/activitylist.vue';
import activitydelete from '../components/activitydelete.vue';
import permissionlist from '../components/permissionlist.vue';
import activityreview from '../components/activityreview.vue';
import activitydetail from '../components/activitydetail.vue';
import register from '../components/register.vue';
import adduser from '../components/adduser.vue';

Vue.use(Router)

const routes = [
    {
      path: '/',
      redirect: { name: 'Home' }
    },
    {
      path: '/',
      name: 'Home',
      component: Home,
      meta: { requiresAuth: false }
    },
    {
      path: '/Login',
      name: 'Login',
      component: Login,
      meta: { requiresAuth: false }
    },
    {
      path: '/activitypost',
      name: 'activitypost',
      component: activitypost,
      meta: { requiresAuth: true }

    },
    {
      path: '/superadmin',
      name: 'superadmin',
      component: superadmin,
      meta: { requiresAuth: true }
    },
    {
      path: '/admin',
      name: 'admin',
      component: admin,
      meta: { requiresAuth: true }
    },
    {
      path: '/user',
      name: 'user',
      component: user,
      meta: { requiresAuth: true }
    },
    {
      path: '/activitylist',
      name: 'activitylist',
      component: activitylist,
      meta: { requiresAuth: true }
    },
    {
      path: '/activitydelete',
      name: 'activitydelete',
      component: activitydelete,
      meta: { requiresAuth: true }
    },
    {
      path: '/permissionlist',
      name: 'permissionlist',
      component: permissionlist,
      meta: { requiresAuth: true }
    },
    {
      path: '/activityreview',
      name: 'activityreview',
      component: activityreview,
      meta: { requiresAuth: true }
    },
    {
      path:'/activitydetail',
      name:'activitydetail',
      component:activitydetail,
      meta: { requiresAuth: false }
    },
    {
      path:'/register',
      name:'register',
      component:register,
      meta: { requiresAuth: false }
    },
    {
      path:'/adduser',
      name:'adduser',
      component:adduser,
      meta: { requiresAuth: true }
    }
  ]
  // 创建路由器实例
const router = new Router({
  mode: 'history', // 使用history模式
  base: process.env.BASE_URL,
  routes,
});

// 使用常量存储Token的key
const TOKEN_KEY = 'authToken';

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const hasToken = localStorage.getItem(TOKEN_KEY);
  console.log('Router Token:', hasToken); 
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (hasToken) {
      next(); // 如果存在Token，允许访问
    } else {
      // 如果没有Token，重定向到登录页，并保留原始路由的查询参数
      const query = from.query;
      next({
        path: '/login',
        query: { ...query, redirect: to.path } // 合并原始查询参数和redirect
      });
    }
  } else {
    next(); // 如果路由不需要Token验证，直接放行
  }
});

export default router;
